define(['../../../js/lib/other/sweet-alert'],function() {
    return ['$scope','$location', '$interval','$timeout','$route','bizProxy', function($scope,$location,$interval,$timeout,$route,bizProxy) {
		$scope.$emit("top-show",'true');//顶部显示
    	$scope.$emit('menuTypeLight','mine'); 	//控制底部menu高亮
    	$scope.$emit('bub-menu','false'); 	//显示底部menu
    	$scope.$emit('flag-tag','4-1');//显示底部文字
    	
    	//消息推送样式
    	judgeMessagePoint();
    	$scope.$on("mineListMessagePoint",function(event,data){
    		//接收推送消息并查询信息查询接口
    		$timeout(function(){
    			$scope.showPoint = true;
                queryMessage();
    		},0);
		});

        queryMessage();
    	if(resultData.loginUserData.isAgency == 'Y'){
			$scope.isShowAgency = false;
		}else{
			$scope.isShowAgency = true;
		}
    	
    	if(_YHC_isWEIXIN.isWeiXin()){
			$scope.avatarImage = "background-image:url("+transData.authInfo.avatar+")";
		}else{
			$scope.avatarImage = "background-image:url(img/head_portrait.png)";
		}
    	$scope.userName=resultData.loginResultData.nickName;
        $scope.accountInfo=function () {
            $scope.$emit("goNext","/accountInfo");
        };
    	
    	//清空相关数据
    	transData.pwd1 = "",transData.pwd2 = "",transData.mobileCheckNo = "";
    	
    	//默认值
    	$scope.isShow_block = false;
    	$scope.userNameData = resultData.loginUserData;
		$scope.mobileNo = transData.mobileNo;
		var tempFlag = '0';
		
    	//点击显示更改密码和退出登录
    	$scope.openBlock = function(){
    		if(tempFlag === '0'){
    			$scope.isShow_block = true;
    			$timeout(function(){
    				tempFlag = '1';
    			},500);
    		}
    	}
    	
    	//点击关闭更改密码和退出登录
    	$scope.closeDiv = function(){
    		if(tempFlag === '1'){
    			$scope.isShow_block = false;
    			$timeout(function(){
    				tempFlag = '0';
    			},500);
    		}
    		
    	}
        //点击风险投查询
        $scope.queryInvest = function(){
            if(resultData.loginUserData.userName){
                $scope.$emit("goNext","/queryInvest");
            }else{//未完善资料用户不可申请
                $scope.$emit("promptAlertMsg","请先去'我的'里面完善个人信息");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
            }
        }
    	//点击分享按钮
    	$scope.openShare = function(){
    		var data = {};
    		data.userId = transData.userId;
    		data.userName = resultData.loginUserData.userName;
    		data.mobileNo = transData.mobileNo;
    		//alert("邀请朋友")
    		share.shareForFriends("给您一个机会，为弱势群体点亮希望！足不出户，分享律师财富增值！","赢火虫推广计划等您加盟！",JSON.stringify(data),null,null);
    	}
    	
    	//点击更改密码
    	$scope.changePwd = function(){
    		$scope.isShow_block = false;
    		$scope.$emit("goNext","/modifyPwd");
    	}
    	
    	//点击退出登录
    	$scope.quickLogin = function(){
    		$scope.isShow_block = false;
    		userLogout();
    	}
    	
    	//点击修改账户
    	$scope.changeMobile = function(){
    		$scope.isShow_block = false;
    		$scope.$emit("goNext","/changeMobile");
    	}
    	
    	//点击查看信息
    	$scope.openLetter = function(){
    		$scope.$emit("goNext","/msgCenter");
    	}
    	
    	//点击收藏
    	$scope.collectionFn = function(){
    		$scope.$emit("goNext","/myCollection");
    	}
        //点击消息
        $scope.messageFn = function(){
            $scope.$emit("goNext","/messageIndex");
        }
    	
    	//点击我的二维码
    	$scope.goQrcode = function(){
    		$scope.$emit("goNext","/myQrCode");
    	}
    	
    	//点击我的收益
    	$scope.goIncome = function(){
    		$scope.$emit("goNext","/myIncome");
    	}
    	
    	//点击代理商申请
    	$scope.applyAgent = function(){
    		$scope.$emit("goNext","/applyAgent");
    	}
    	
    	//点击关于我们
    	$scope.aboutUs = function(){
    		$scope.$emit("goNext","/aboutUs");
    	}
    	
    	//点击帮助
    	$scope.helpUs = function(){
    		$scope.$emit("goNext","/helpUs");
    	}
    	
    	//点击个人信息
    	$scope.goPersonalInfo = function(){
    		if(resultData.loginUserData != "" && resultData.loginUserData != null){
				if(resultData.loginUserData.idNo != "" && resultData.loginUserData.idNo != null){
					$timeout(function(){
						$scope.$emit("goNext","/personalInfo");
					},300);
				}else{
					$timeout(function(){
						// $scope.$emit("goNext","/completePtData");
						$scope.$emit("goNext","/completeData");
					},300);
				}
			}else{
				$timeout(function(){
					//$scope.$emit("goNext","/completePtData");
                    $scope.$emit("goNext","/completeData");
				},300);
			}
    	}
    	
    	//点击去申请
    	$scope.goApplyfor = function(){
    		$scope.$emit("goNext","/fireLoanForm");
    	}
    	
    	//查看全部贷款
    	$scope.checkLoan = function(status,type){
    		MY_PUSH_TYPE[type] = false;
    		$scope.myTypeData = MY_PUSH_TYPE;
    		//$scope.$emit("redPointFlag",forEachMyType());
    		//0待律师审核；1待平台审核；2待放款；3还款中；4已还清；5已逾期；8已驳回、9已取消
    		if(status === "3"){
    			$scope.$emit("goNext","/loanRepaying/"+status);
    		}else{
    			$scope.$emit("goNext","/myLoanList/"+status);
    		}
    		
    	}
    	
    	//点击我的发布
    	$scope.myIssue = function(status,type){
    		MY_PUSH_TYPE[type] = false;
    		$scope.myTypeData = MY_PUSH_TYPE;
    		//$scope.$emit("redPointFlag",forEachMyType());
    		//0待审核；1已通告；8已驳回；9已取消
    		if(status === '8'){
    			$scope.$emit("goNext","/issueFailList/"+status);
    		}else{
    			$scope.$emit("goNext","/myIssueList/"+status);
    		}
    		
    	}
    	
    	//退出登录
    	function userLogout(){
    		var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum = '';
        	headerInfo.pageSize = '';
        	headerInfo.sessionId = transData.sessionId;
    		
        	bizInfo.mobileNo = transData.mobileNo;
    		$scope.$emit("loadingChange",true);
    		bizProxy.queryProxyBizData("userLogout",headerInfo,bizInfo,
    				function(tempData){//SUCCESS
    					$scope.$emit("loadingChange",false);
		    			transData.mobileNo = "";
						transData.password = "";
						transData.sessionId = "";
						transData.userType = "";
						resultData.loginUserData = "";
						resultData.loginUserData = "";
						$timeout(function(){
							$scope.$emit("goNext","/mine");
						},0);
		    		},function(data){//FAIL
		    			$scope.$emit("loadingChange",false);
		    			$scope.$emit("promptAlertMsg","连接超时");
		        		$scope.$emit("showCollect",true);
						$timeout(function(){
							$scope.$emit("showCollect",false);
						},transData.promptTime);
		    		});
    	}
    	
    	//根据缓存是否显示推送消息红点
    	function judgeMessagePoint(){
    		var tempMessage = localStorage.getItem("firflyMessageInfo");
    		if(!tempMessage){
    			$scope.showPoint = false;
        	}else{
        		var tempJsonData = JSON.parse(tempMessage);
        		for(var i=0;i< tempJsonData.length;i++){
        			if(tempJsonData[i].readStatus == '1'){
        				$scope.showPoint = true;
        				break;
        			}
        		}
        	}
    	}
    	
    	//消息查询
        function queryMessage(){
        	var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum = '';
        	headerInfo.pageSize = '';
        	headerInfo.sessionId = transData.sessionId;
        	
        	var tempLastMessageId = localStorage.getItem("firflyLastMessageId");
        	if(tempLastMessageId){
        		bizInfo.lastMessageId = tempLastMessageId;
        	}else{
        		bizInfo.lastMessageId = "";
        	}
    		// $scope.$emit("loadingChangeNav",true);
    		bizProxy.queryProxyBizData("queryMessage",headerInfo,bizInfo,
    				function(tempData){//SUCCESS
                        queryDdVersionList()
		    			if(tempData.body != "" && tempData.body != null && tempData.body != "null" && tempData.body != "[]"){
		    				console.log("消息查询="+JSON.stringify(tempData))
							var tempMsgList = JSON.parse(tempData.body);
							$timeout(function(){
								// $scope.$emit("loadingChangeNav",false);
								checkData(tempMsgList);
								if(tempMsgList.length>0){
                                    $scope.showPoint = true;
								}
							},0);
						}else{
							$timeout(function(){
								// $scope.$emit("loadingChangeNav",false);
								checkData(new Array());
							},0);
						}
		    		},function(data){//FAIL
                    	queryDdVersionList()
		    			// $scope.$emit("loadingChangeNav",false);
		    			$timeout(function(){
		    				checkData(new Array());
		    				$scope.$emit("promptAlertMsg",data.errorMsg);
							$scope.$emit("showCollect",true);
							$timeout(function(){
							},transData.promptTime);
						},0);
		    		});
        }
        
        //分类处理数据
        function checkData(list){
    		var contactArryData;
    		//保存message信息在localstorage中
        	var tempMessage = localStorage.getItem("firflyMessageInfo");
        	if(!tempMessage && list.length == 0){
        		return;
        	}
        	if(!tempMessage){
        		localStorage.setItem("firflyMessageInfo",JSON.stringify(list));
        		contactArryData = list;
        	}else{
        		var tempJsonData = JSON.parse(tempMessage);
        		contactArryData = list.concat(tempJsonData);
        		localStorage.setItem("firflyMessageInfo",JSON.stringify(contactArryData));
        	}
            var messageIdList=[];
            contactArryData.forEach(function(v){
                messageIdList.push(v.messageId);
            });
            //保存最大messageId
            if(list.length > 0){
                var lastMessageId = Math.max.apply(null, messageIdList);
                localStorage.setItem("firflyLastMessageId",lastMessageId);
            }
        }
        function queryDdVersionList(){
            if(transData.stl_baseData){
                return;
            }else {
                transData.stl_baseData=true;
            }
            var headerInfo = {},bizInfo = {};
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId = transData.sessionId;
            bizProxy.queryProxyBizData("queryDdVersionList", headerInfo,bizInfo,function(tempData){
                if(tempData.body != '{}'){
                    console.log("数字字典版本号",JSON.parse(tempData.body));
                    $timeout(function(){
                        var list = JSON.parse(tempData.body).ddVersionList;
                        var storage_DdVersionList = localStorage.getItem("storage_DdVersionList");
                        if(storage_DdVersionList){
                        	try{
                        		var cacheDdVersionData = JSON.parse(storage_DdVersionList);
                                var arr = [];
                                for (var i = 0; i < list.length; i++) {
                                    for (var j = 0; j < cacheDdVersionData.length; j++) {
                                        if (list[i].ddName == cacheDdVersionData[j].ddName) {
                                            if (parseInt(list[i].versionNo) > parseInt(cacheDdVersionData[j].versionNo)) {
                                            	arr.push(list[i].ddName==='caseType'?'queryCaseTypeList':list[i].ddName==='caseStage'?'queryCaseStageList':list[i].ddName==='fundUse'?'queryFundUseList':list[i].ddName==='advField'?'queryAdvFieldList':'queryDocKindList')
                                            }
                                        }
                                    }
                                }
                                localStorage.setItem("storage_DdVersionList", JSON.stringify(list));
                                if(arr.length>0){
                                    getBaseData(arr)
                                }
                        	}catch(e){
                        		 $timeout(function(){
                                     localStorage.setItem("storage_DdVersionList",JSON.stringify(list));
                                     getBaseData(['queryCaseTypeList','queryCaseStageList','queryFundUseList','queryDocKindList','queryAdvFieldList'])
                                 },0);
                        	}
                        }else {
                            $timeout(function(){
                                localStorage.setItem("storage_DdVersionList",JSON.stringify(list));
                                getBaseData(['queryCaseTypeList','queryCaseStageList','queryFundUseList','queryDocKindList','queryAdvFieldList'])
                            },0);
                        }
                    },0);
                }
            },function(data){
                if(data.errorMsg){
                    $scope.$emit("loadingChange",false);
                    $scope.$emit("promptAlertMsg",data.errorMsg);
                    $scope.$emit("showCollect",true);
                    $timeout(function(){
                        $scope.$emit("showCollect",false);
                    },transData.promptTime);
                }
            });
        }
        var getBaseData=function (list) {
            var step=0;
            var exec=function () {
                getBaseData_query(list[step],function (next) {
                    if(next){
                        exec()
                    }
                },list[++step])
            }
            exec()
        };
        var getBaseData_query=function(name,suCallBack,next){
            var headerInfo = {},bizInfo = {};
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId = transData.sessionId;
            bizProxy.queryProxyBizData(name,headerInfo,bizInfo,function(tempData){
                    if(tempData.body != '{}'){
                        $timeout(function(){
                            console.log(name+' : ',JSON.parse(tempData.body));
                            transData.storage_baseData[name]=JSON.parse(tempData.body);
                            localStorage.setItem('storage_baseData',JSON.stringify(transData.storage_baseData));
                            suCallBack(next);
                        },0);
                    }
                },
                function(data){
                    console.log(data);
                    if(data.errorMsg){
                        $scope.$emit("loadingChange",false);
                        $scope.$emit("promptAlertMsg",data.errorMsg);
                        $scope.$emit("showCollect",true);
                        $timeout(function(){
                            $scope.$emit("showCollect",false);
                        },transData.promptTime);
                    }
                });
        }
        $scope.$apply();
    }];
});